package com.draby.starfantasymaze.activities.main.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.draby.starfantasymaze.R;
import com.draby.starfantasymaze.utils.FileUtil;

public class RecordFragment extends Fragment {

    // 文件存放父目录与文件名
    private String parent;
    private String data_fileName;

    // 可能用到的控件
    private TextView tv_record_easy;
    private TextView tv_record_normal;
    private TextView tv_record_hard;
    private TextView tv_record_infinite;
    private TextView tv_infinite_distance;
    private TextView tv_record_birthday;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_record, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 设置文件存放父目录与文件名
        parent = getString(R.string.file_directory);
        data_fileName = getString(R.string.fileName_data);

        // 找到控件
        tv_record_easy = view.findViewById(R.id.tv_record_easy);
        tv_record_normal = view.findViewById(R.id.tv_record_normal);
        tv_record_hard = view.findViewById(R.id.tv_record_hard);
        tv_record_infinite = view.findViewById(R.id.tv_record_infinite);
        tv_infinite_distance = view.findViewById(R.id.tv_infinite_distance);
        tv_record_birthday = view.findViewById(R.id.tv_record_birthday);

        // 设置文本初始值
        setInitValue();
    }

    /**
     * 设置初始文本值
     */
    private void setInitValue() {
        tv_record_easy.setText(FileUtil.getValue(parent, data_fileName, "recordEasy"));
        tv_record_normal.setText(FileUtil.getValue(parent, data_fileName, "recordNormal"));
        tv_record_hard.setText(FileUtil.getValue(parent, data_fileName, "recordHard"));
        tv_record_infinite.setText(FileUtil.getValue(parent, data_fileName, "recordInfinite"));
        tv_infinite_distance.setText(FileUtil.getValue(parent, data_fileName, "infiniteDistance"));
        tv_record_birthday.setText(FileUtil.getValue(parent, data_fileName, "recordBirthday"));
        // 使文本框处于选中状态，保证跑马灯效果
        tv_record_easy.setSelected(true);
        tv_record_normal.setSelected(true);
        tv_record_hard.setSelected(true);
        tv_record_infinite.setSelected(true);
        tv_infinite_distance.setSelected(true);
        tv_record_birthday.setSelected(true);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            setInitValue();
        }
    }
}
